package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import model.Student;
import control.StudentDAO;

public class Add extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField tfId;
	private JTextField tfName;
	private JTextField tfAge;
	private JTextField tfSex;
	private JTextField tfHomeTown;
	private JButton btAdd,btExit;
	//String actionAdd="Add",actionBack="Back";
	public Add(){
		add(createMainPanel());
		//set display
		setTitle("Add");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		//setVisible(true);
		
	}
	public JPanel createMainPanel(){
		JPanel panel =new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.add(createInputPanel(),BorderLayout.CENTER);
		panel.add(createButtonPanel(),BorderLayout.PAGE_END);
		return panel;
	}
	public JPanel createButtonPanel(){
		JPanel panel=new JPanel();
		panel.add(btAdd=createButton("Add"));
		panel.add(btExit=createButton("Exit"));
		//btAdd.addActionListener(this);
		//btExit.addActionListener(this);
		return panel;
	}
	public JPanel createInputPanel(){
		int col=15;
		JPanel panel=new JPanel(new GridLayout(5, 5, 5, 5));
		panel.add(createLabel("Id"));
		panel.add(tfId=createTextField(col));
		panel.add(createLabel("Name"));
		panel.add(tfName=createTextField(col));
		panel.add(createLabel("Age"));
		panel.add(tfAge=createTextField(col));
		panel.add(createLabel("Sex"));
		panel.add(tfSex=createTextField(col));
		panel.add(createLabel("HomeTown"));
		panel.add(tfHomeTown=createTextField(col));
		return panel;
	}
	public JLabel createLabel(String text){
		JLabel lb=new JLabel(text);
		return lb;
	}
	public JTextField createTextField(int col){
		JTextField tf=new JTextField(col);
		return tf;
	}
	public JButton createButton(String text){
		JButton bt=new JButton(text);;
		bt.addActionListener(this);
		return bt;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JButton bt=(JButton) e.getSource();
		if(bt.equals(btAdd)){
			btAdd_actionperformed();
		}else if(bt.equals(btExit)){
			btExit_actionperformed();
		}
	}
	public void btAdd_actionperformed(){
		Student st=new Student();
		st.setId(Integer.parseInt(tfId.getText()));
		st.setName(tfName.getText());
		st.setAge(Integer.parseInt(tfAge.getText()));
		st.setSex(tfSex.getText());
		st.setHomeTown(tfHomeTown.getText());
		StudentDAO sd=new StudentDAO();
		sd.addStudent(st);
		JOptionPane.showMessageDialog(null, "Add student successfully!");
	}
	public void btExit_actionperformed(){
		new MenuForm().setVisible(true);
		setVisible(false);
	}
}
